<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	String customer_id = request.getParameter("customer_id") == null ? "" : request.getParameter("customer_id");
	String customer = request.getParameter("customer") == null ? "" : request.getParameter("customer");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建项目支付":"编辑项目支付" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
	 var loadUrl='<%=contextPath %>/springMVC/xm/getXM?xm_id=<%=xm_id%>';
	 var submitUrl = '<%=contextPath %>/springMVC/xmFinance/addXMEndPayMain';
	 var url = '<%=contextPath %>/springMVC/xmFinance/listXMWorkersBalanceByXMId?xm_id=<%=xm_id%>';
		$(function(){
			$('#pay_value').numberbox('disable',true);
		   $.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/xmFinance/hasXMEndPayMain?pay_type=1&xm_id=<%=xm_id%>',
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						url = '<%=contextPath %>/springMVC/xmFinance/listXMEndPayMain?pay_type=1&xm_id=<%=xm_id%>';
						submitUrl = '<%=contextPath %>/springMVC/xmFinance/updateXMEndPayMain';
						loadUrl='<%=contextPath %>/springMVC/xmFinance/getXmEndPayMain?pay_type=1&xm_id=<%=xm_id%>';
						$('#submitValue').html("重新保存");
					}
				}
			});
		   $('#ff').form({
				onLoadSuccess:function(data){
				     $.ajax({
					    	type:'POST',
					    	url:url,
					    	success:function(rtJson){
							    if(rtJson != null && rtJson.length>0){
							    	rtJson = eval(rtJson);
						    		$('#person').val(rtJson[0].creator);
							    }
					    	}
					    });
				}
			});
			  $('#ff').form('load',loadUrl);
	       $('#table').datagrid({
	    	    url:'<%=contextPath %>/springMVC/xmFinance/listXMWorkersBalanceByXMId?xm_id=<%=xm_id%>',
	    		rownumbers:true,
				fitColumns:true,
				singleSelect:true,
				frozenColumns:[[
				    {title:'工单支付id',field:'bus_id',hidden:true}
				]],				
				columns:[[
					{title:'工单id',field:'gd_id',hidden:true},
					{title:'分包id',field:'gd_worker_id',hidden:true},
					{title:'分包商',field:'gd_worker_name',width:250,align:'center'},
					{title:'工单编号',field:'gd_code',width:250,align:'center'},
					{title:'工单名称',field:'gd_name',width:350,align:'center'},
					{title:'工单应付',field:'actual_pay_total',width:200,align:'center'},
					{title:'材料扣款',field:'worker_material_money',width:200,align:'center'},
					{title:'实际应付',field:'worker_pay',width:200,align:'center'},
					{title:'进度已付',field:'already_pay',width:200,align:'center'},
					{title:'欠垫款金',hidden:true,field:'borrow_pay',width:200,align:'center'},	
					{title:'当前利息金',hidden:true,field:'interest',width:200,align:'center'},
					{title:'结算扣款',field:'cut_payment',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2,
							min:0
						}
					}},		
					{title:'实际结算支付',field:'end_pay',width:200,align:'center',formatter:function(value,rowData,index){
						if(isNaN(rowData.worker_pay)){
							rowData.worker_pay=0;
						}
						if(isNaN(rowData.already_pay)){
							rowData.already_pay=0;
						}
						if(isNaN(rowData.borrow_pay)){
							rowData.borrow_pay=0;
						}						
						if(isNaN(rowData.interest)){
							rowData.interest=0;
						}
						if(isNaN(rowData.cut_payment)){
							rowData.cut_payment=0;
						}													
					
					//rowData.end_pay=parseFloat(rowData.worker_pay)-parseFloat(rowData.already_pay)-parseFloat(rowData.borrow_pay)-parseFloat(rowData.interest)-parseFloat(rowData.cut_payment);
					rowData.end_pay=parseFloat(rowData.worker_pay)-parseFloat(rowData.already_pay)-parseFloat(rowData.cut_payment);
					return rowData.end_pay.toFixed(2);
					}},	
												
					{title:'备注',field:'remark',width:200,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(param){
							
					$(this).datagrid('rejectChanges');
					
				},
				onAfterEdit:function(rowIndex, rowData, changes){
				calculaMoney();
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'cut_payment'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				onLoadSuccess:function(rtJson){
					calculaMoney();
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						calculaMoney();	
	
					}
					}
			   ]
			});
			
		});

			function calculaMoney(){
				var inMoneys=0;
				var cut_payment=0;
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					
				inMoneys+=parseFloat(jsondata.rows[i].end_pay);	
				
				}
				$('#pay_value').numberbox('setValue', inMoneys);
		}	
		function addXMPaymentWorker(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			var xm_id =jQuery("#xm_id").val();	
			var xm_name=jQuery("#xm_name").val();	
			var xm_code=jQuery("#xm_code").val();
			var pay_value=jQuery("#pay_value").val();
			var remark=encodeURIComponent(jQuery('#remark').val());
			var customer=encodeURIComponent(jQuery('#customer').val());
			var admin_dept=jQuery("#admin_dept").val();
			var admin_dept_id=jQuery("#admin_dept_id").val();
			if(!checkNullOREmpty(xm_id,'项目为必填项')){return;}
			
			var gds="";
			var jsondata=$("#table").datagrid("getData");
			for(var i=0;i<jsondata.total;i++){
				if(gds!=""){
					gds+=";";
				}
				gds+=jsonToStr(jsondata.rows[i]);
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				//|| parseFloat(jsondata.rows[i].end_pay) < 0
				if(jsondata.rows[i].end_pay == null  ){
					$.messager.alert('提示','第'+index+'行支付金额有误！');
					return ;
				}
				
			}
			var param = "pay_type=1&remark="+remark+"&customer="+customer+"&admin_dept="+admin_dept+"&admin_dept_id="+admin_dept_id+"jsondata="+jsondata
			+"&pay_value="+pay_value+"&pay_to_id="+pay_to_id+"&pay_to_name="+pay_to_name
			+"&xm_id="+xm_id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&gds="+gds;
			$(obj).hide();
			$.ajax({
				type:'POST',
				url:submitUrl,
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						$.messager.alert("提示",rtJson.rtMsrg);
						//submitUrl = '<%=contextPath %>/springMVC/xmFinance/updateXMEndPayMain';
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					//$(obj).show();
				}
			});
		}

	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
		<input  type="hidden" id="pay_to_id" name="pay_to_id" >
		<input  type="hidden" id="pay_to_name" name="pay_to_name" >
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>">
						<input type="hidden" id="xm_id" name="xm_id" readOnly value="<%=xm_id%>">
	
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>项目部门：</td>
					<td class="td2" nowrap>
						<input  id="admin_dept" name="admin_dept" readOnly value="">
						<input  type="hidden" id="admin_dept_id" name="admin_dept_id" readOnly value="">
					</td>
					<td align="right" class="td1" nowrap>经办人：</td>
					<td class="td2" nowrap>
						<%if(!"1".equals(isShow)){ %>
							<input  id="person" name="person" readOnly value="<%=person%>">
						  <%} else{%>
						  	<input  id="person" name="person" readOnly >
						  <%}%>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>对应客户：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="customer" name="customer" readOnly value="">
						<input  type="hidden" id="customer_id" name="customer_id" readOnly value="">
					</td>
				</tr>
				
				<tr>	
			        <td align="right" class="td1" nowrap>质保金：</td>
					<td class="td2" nowrap  >
						<input  id="warranty" name="warranty" class="easyui-numberbox" data-options="min:0,precision:2" value="0" > 
					</td>
	  				<td align="right" class="td1" nowrap>付款金额：</td>
					<td class="td2" nowrap  >
						<input  id="pay_value" name="pay_value" class="easyui-numberbox" data-options="min:0,precision:2" value="0" >(自动计算)
					</td>
			       
				</tr>				
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
						   <%if(!"1".equals(isShow)){ %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addXMPaymentWorker(this);"><span id="submitValue">提交保存</span></a>
							<%} %>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>